﻿///<reference path="dom.js" />

(function() {
		var E = Sys.UI.DomEvent;

		var Handle = function(element, type, handler, data) {
			this.element = element;
			this.type = type;
			this.handler = handler;
			this.data = data || false;
		}

		var Point = function(x, y) {
			this.x = x;
			this.y = y;
		}



		amp["class"]("amp.UI.Point", null, Point);

		var Event = {
			add: function(element, type, handler, data) {
				var result = new Array(), elements = null,
					elements = amp.array(element);

				handler = amp.extend(handler, data);

				for (var i = 0; i < elements.length; i++) {
					var el = elements[i], handle = new Handle(el, type, handler, data);
					result.push(handle);
					if (el != window)
						el[type] = handle;
					else
						amp.Cache.window[type] = handle;

					$addHandler(el, type, handler);
				}
				return (result.length == 1) ? result[0] : result;
			},

			pageX: function(event) {
				var x = event.pageX;
				if (!x && 0 !== x) {
					x = event.clientX || 0;

					if (amp.Browser.Msie) {
						x += getScrollLeft();
					}
				}

				return x;
			},

			pageY: function(event) {
				var y = ev.pageY;
				if (!y && 0 !== y) {
					y = ev.clientY || 0;

					if (amp.Browser.Msie) {
						y += getScrollTop();
					}
				}
				return y;
			},

			onDomReady: function(callback) {
				Sys.Application.add_load(callback);
			},

			getTarget: function(event) {
				var t = event.target || event.srcElement;
				return t;
			},

			get: function(e, boundEl) {
				var ev = e || window.event;

				if (!ev) {
					var c = this.getEvent.caller;
					while (c) {
						ev = c.arguments[0];
						if (ev && Event == ev.constructor) {
							break;
						}
						c = c.caller;
					}
				}

				return ev;
			},

			charCode: function(ev) {
				var temp = new E(ev);
				return temp.charCode;
			},

			preventDefault: function(ev) {
				new E(ev).preventDefault();
			},

			stopPropagation: function(ev) {
				new E(ev).stopPropagation();
			},

			stop: function(event) {
				this.stopPropagation(event);
				this.preventDefault(event);
			},

			cordinates: function(event) {
				return new amp.Point(this.pageX(event), this.pageY(event));
			},

			fire: function(element, eventName, data) {

				element = amp.byId(element);
				if (element == document && document.createEvent && !element.dispatchEvent)
					element = document.documentElement;

				if (document.createEvent) {
					var event = document.createEvent("HTMLEvents");
					event.initEvent("dataavailable", true, true);
				} else {
					var event = document.createEventObject();
					event.eventType = "ondataavailable";
				}

				event.eventName = eventName;
				event.data = data || {};

				if (document.createEvent) {
					element.dispatchEvent(event);
				} else {
					element.fireEvent(event.eventType, event);
				}

				return event;
			},

			remove: function(element, type) {
				var elements = amp.array(element), element = null, handle = null;


				for (var i = 0; i < elements.length; i++) {
					handle = elements[i];

					if (!handle.handler)
						handle = element[type];
					$removeHandler(handle.element, handle.type, handle.handler);
					if (handle.element == window)
						delete amp.Cache.window[handle.type];
					else
						delete handle.element[handle.type];
				}
				return;
			}
		}
		amp.event = Event;

		amp.extend(amp, {
			fire: Event.fire,
			init: Event.onDomReady
		});

		amp.extend(amp.UI.Dom.prototype, {
			fire: function(type, data) {
				this.elements.invoke(amp.event.fire, type, data);
			},

			bind: function(type, handler, data) {
				amp.event.add(this.get(), type, handler, data);
			},

			unbind: function(type) {
				amp.event.remove(this.get(), type);
			}
		});

		("blur,focus,load,resize,scroll,unload,click,dblclick," +
			"mousedown,mouseup,mousemove,mouseover,mouseout,change,select," +
			"submit,keydown,keypress,keyup,error"
			).split(",").each(function(type) {
			amp.UI.Dom.prototype["on" + type] = function(handler, data) {
				this.elements.invoke(function(item) {
					amp.event.add(item, type, handler, data);
				});
			}
		});


	})();